<template>
  <div class="container">
    <div class="center-container">
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px"
               class="demo-userForm">
        <el-form-item label='附件' prop='fileList'>
          <el-upload class='m-upload'
                     action='/mock/user/img/upload'
                     :headers='headers'
                     accept='.jpg,.png,.jpeg'
                     :on-success='handleAvatarSuccess'
                     :file-list='userForm.fileList'
                     :limit='1'
                     :on-exceed='handleExceed'
                     :on-preview='handlePreview'
                     :on-remove='handleRemove'
                     :before-upload="beforeAvatarUpload"
                     :before-remove="beforeRemove"
          >
            <el-button type='primary'>上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('userForm')">保 存</el-button>
          <el-button @click="resetForm('userForm')">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      @close="closeDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center>
      <span><pre>{{ userForm }}</pre></span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import VueCookies from 'vue-cookies';

export default {
  name: "validate-01",
  data() {
    return {
      userForm: {
        fileList: []
      },
      rules: {
        fileList: [
          {type: 'array', required: true, message: '请上传附件', trigger: ['change', 'blur']},
          {type: 'array', min: 1, message: '请至少上传1个附件', trigger: 'change'},
        ],
      },

      centerDialogVisible: false,
    };
  },
  computed: {
    headers() {
      return {
        'Authorization': 'Bearer ' + VueCookies.get('access_token') // 直接从本地获取token就行
      };
    },
  },
  methods: {
    closeDialog() {
      this.centerDialogVisible = false;
      this.$refs.userForm.resetFields();
    },
    submitForm() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.centerDialogVisible = true;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.userForm.resetFields();
    },
    handleAvatarSuccess(response, file, fileList) {
      //上传成功
      console.log("response:", JSON.stringify(response))
      let self = this;
      let fileInfo = {
        ...response.data
      };
      fileInfo.name = fileInfo.fileName;
      self.userForm.fileList.push(fileInfo);
      this.$refs.userForm.validateField("fileList")
      this.$forceUpdate();
    },
    handleExceed(files, fileList) {
      this.$message.warning('当前限制选择 2 个文件，本次选择了' + files.length + '个文件，共选择了' + (files.length + fileList.length) + ' 个文件');
    },
    handlePreview(val) {
      console.log("val:", JSON.stringify(val))
    },
    handleRemove() {
      this.userForm.fileList = [];
    },
    beforeAvatarUpload(file) {
      console.log("fileLength", file.name.length)
      if (file.name.length > 8) {
        this.$message.error('文件名称不能大于8');
        return false;
      }
      console.log(file.name)
      console.log("file:", JSON.stringify(file))
    },
    beforeRemove() {
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 设置父容器的高度为视口高度，使其充满整个屏幕 */
}

.center-container {
  max-width: 600px; /* 设置内部容器的最大宽度，根据需求进行调整 */
}
</style>
